package com.imooc.miaosha.controller.FeController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.gson.JsonObject;
import com.imooc.miaosha.common.Const;
import com.imooc.miaosha.dto.UserDTO;
import com.imooc.miaosha.pojo.Shipping;
import com.imooc.miaosha.service.ShippingService;
import com.imooc.miaosha.util.CookieUtil;
import com.imooc.miaosha.util.JedisUtil;
import com.imooc.miaosha.util.JsonData;
import com.imooc.miaosha.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/shipping")
public class ShippingController {
    @Autowired
    ShippingService shippingService;

    @RequestMapping("list_mini")
    public JsonData list(String token) {
        String userJson = JedisUtil.get(Const.LOGIN_TOKEN_PREFIX, token);
        UserDTO userDTO = JsonUtil.String2Obj(userJson, new TypeReference<UserDTO>() {
        });
        Long userId = userDTO.getId();
        if (userId == null) {
            return JsonData.fail("用户未登录,无法显示地址信息");
        }
        String shippingJson = JedisUtil.get("shipping", userId.toString());

        if (shippingJson != null) {
            return JsonData.success(JsonUtil.String2Obj(shippingJson, new TypeReference<List<Shipping>>() {
            }));
        }
        List<Shipping> shippingList = shippingService.shippingList(userId);
        JedisUtil.setEx("shipping", userDTO.getId().toString(), JsonUtil.Obj2String(shippingList), 60);
        return JsonData.success(shippingList);

    }

    @RequestMapping("list")
    public JsonData list(HttpServletRequest request, Model model) {
        String loginToken = CookieUtil.readCookie(request);
        String userJson = JedisUtil.get(Const.LOGIN_TOKEN_PREFIX, loginToken);
        UserDTO userDTO = JsonUtil.String2Obj(userJson, new TypeReference<UserDTO>() {
        });
        if (userDTO == null) {
            return JsonData.fail("用户未登录,无法显示地址信息");
        }
        String shippingJson = JedisUtil.get("shipping", userDTO.getId().toString());

        if (shippingJson != null) {
            return JsonData.success(JsonUtil.String2Obj(shippingJson, new TypeReference<List<Shipping>>() {
            }));
        } else {
            List<Shipping> shippingList = shippingService.shippingList(userDTO.getId());
            JedisUtil.setEx("shipping", userDTO.getId().toString(), JsonUtil.Obj2String(shippingList), 60);
            return JsonData.success(shippingList);
        }
    }

    @RequestMapping("add_mini")
    public JsonData add_mini(@RequestBody Map<String, Object> shippingTokenMap) throws IOException {
        String token = (String) shippingTokenMap.get("token");
        Shipping shipping = JsonUtil.map2Obj(shippingTokenMap, "shipping", Shipping.class);
        String userJson = JedisUtil.get(Const.LOGIN_TOKEN_PREFIX, token);
        UserDTO userDTO = JsonUtil.String2Obj(userJson, new TypeReference<UserDTO>() {
        });
        List<Shipping> shippingList = shippingService.add(shipping, userDTO.getId());
        JedisUtil.setEx("shipping", userDTO.getId().toString(), JsonUtil.Obj2String(shippingList), 60);
        return JsonData.success("添加成功");
    }

    @RequestMapping("add")
    public JsonData add(Shipping shipping, HttpServletRequest request) {
        String token = CookieUtil.readCookie(request);
        String userJson = JedisUtil.get(Const.LOGIN_TOKEN_PREFIX, token);
        UserDTO userDTO = JsonUtil.String2Obj(userJson, new TypeReference<UserDTO>() {
        });
        if (userDTO == null) {
            return JsonData.fail("用户未登录或者session过期");
        }
        List<Shipping> shippingList = shippingService.add(shipping, userDTO.getId());
        JedisUtil.setEx("shipping", userDTO.getId().toString(), JsonUtil.Obj2String(shippingList), 60);

        return JsonData.success("添加成功");

    }

    @RequestMapping("delete")
    public JsonData del(HttpServletRequest request, Long shippingId) {
        String loginToken = CookieUtil.readCookie(request);
        String userJson = JedisUtil.get(Const.LOGIN_TOKEN_PREFIX, loginToken);
        UserDTO userDTO = JsonUtil.String2Obj(userJson, new TypeReference<UserDTO>() {
        });
        if (userDTO == null) {
            return JsonData.fail("用户未登录或者session已过期");
        }
        List<Shipping> shippingList = shippingService.del(userDTO.getId(), shippingId);
        JedisUtil.setEx("shipping", userDTO.getId().toString(), JsonUtil.Obj2String(shippingList), 60);
        return JsonData.success("删除成功");
    }

}
